const nodemailer = require('nodemailer');
const config = require("../config");
const logging = require("./log");

/**
 * 开启一个 SMTP 连接池
 */
const transport = nodemailer.createTransport({
    host: config().emil.host,
    secure: config().emil.secure,
    secureConnection: config().emil.secureConnection,
    port: config().emil.port,
    auth: {
        user: config().emil.auth.user,
        pass: config().emil.auth.pass
    }
});

/**
 * 发送邮件服务
 * @param to
 * @param subject
 * @param html
 * @return {Promise<void>}
 */
async function sendEmail(to,subject,html) {
    try {
        const info = await transport.sendMail({
            from: config().emil.from,
            to: to,
            subject: subject,
            html: html
        });
        logging('warn','Message sent success id:'+ info.messageId)
        transport.close();
    } catch (err) {
        logging('error','Message sent fail :'+ err)
    }
}

module.exports=sendEmail;
